﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.CrossCutting.Configuration
{
    public class ConfigManager
    {
        public delegate void EventHandler(Object sender, EventArgs e);
        public static event EventHandler ConfigSourceChangedEventHandler;

        public static void InvokeConfigSourceChangedEventHandler(EventArgs e)
        {
            EventHandler handler = ConfigSourceChangedEventHandler;
            if (handler != null) handler(null, e);
        }

        private static IConfigSource current;

        static ConfigManager()
        {
            Init();
        }

        private static void Init()
        {
            //IList<IConfigSource> sources = new List<IConfigSource>
            //                                   {
            //                                       LocalPublishingConfiguration.Instance,
            //                                       GlobalPublishingConfiguration.Instance
            //                                   };
            //current = new MultiConfigSource(sources);
        }

        //public static void SetConfigChangeEventHandler(EventHandler nConfigSourceChanged)
        //{
        //    Current.OnConfigSourceChanged += nConfigSourceChanged;
        //}

        public static IConfigSource Current
        {
            get { return current; }
        }

        public static object Get(string key)
        {
            return current.Get<object>(key);
        }

        public static string GetString(string key)
        {
            return current.Get<string>(key);
        }

        public static T Get<T>(string key)
        {
            return current.Get<T>(key);
        }

        public static object Get(string section, string key)
        {
            return current.Get<object>(section, key);
        }

        public static string GetString(string section, string key)
        {
            return current.Get<string>(section, key);
        }

        public static T Get<T>(string section, string key)
        {
            return current.Get<T>(section, key);
        }

        public static IConfigSection GetSection(string sectionName)
        {
            return current.GetSection(sectionName);
        }

        public static bool ContainsKey(string key)
        {
            return current.Contains(key);
        }

        public static bool ContainsKey(string sectionName, string key)
        {
            return current.Contains(sectionName, key);
        }

        public static List<string> Sections
        {
            get { return current.Sections; }
        }

        public static string Name
        {
            get { return current.Name; }
        }

        public static string SourcePath
        {
            get { return current.SourcePath; }
        }

        public static EventHandler ConfigChangeEventHandler { get; set; }

        public static void ChangeConfigItem(string configItemName, string value)
        {
            current.ChangeConfigItem(configItemName, value);
        }
    }

}
